#pragma once
#include <vector>
#include <cstdint>

using namespace std;

namespace myAtoi
{
	class Solution {
	public:
		int myAtoi(string s) {
			enum Status {
				StatusPre,
				StatusMid,
				StatusSuffix,
			};
			Status status = StatusPre;
			int sign = 1;
			int result = 0;
			for (int i = 0; i < s.length(); i++)
			{
				char c = s[i];
				if (c == ' ')
				{
					if (status == StatusPre)
					{
						continue;
					}
					else
					{
						return result * sign;
					}
				}
				else if (c == '+' || c == '-')
				{
					if (status == StatusPre)
					{
						status = StatusMid;
						sign = (c == '+' ? 1 : -1);
						continue;
					}
					else
					{
						return result * sign;
					}
				}
				else if (c >= '0' && c <= '9')
				{
					status == StatusMid;
					if (result > INT32_MAX / 10 || (result == INT32_MAX / 10 && (c - '0') > INT32_MAX % 10))
						return sign == 1 ? INT32_MAX : INT32_MIN;
					else
						result = result * 10 + (c - '0');
				}
				else
				{
					return result * sign;
				}
			}
			return result * sign;
		}
	};
}
